package com.bml.architect.consumer.service;

import com.bml.architect.DemoService;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.IOException;

//@EnableAutoConfiguration
public class LoadBalanceConsumer {

//leastactive:最少活跃数
    //roundrobin:轮询
    //random:随机
    //consistenthash :一致性hash
    @Reference(version = "ld", loadbalance = "roundrobin",
            methods = {@Method(name = "sayHello1",loadbalance = "consistenthash")})
    private DemoService demoService;

    public static void main(String[] args) throws IOException {
        ConfigurableApplicationContext context = SpringApplication.run(LoadBalanceConsumer.class);

        DemoService demoService = context.getBean(DemoService.class);

        // 用来负载均衡
        for (int i = 0; i < 5; i++) {
            System.out.println((demoService.sayHello("bml")));
            try {
                Thread.sleep(1 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("=============================");
        for (int i = 0; i < 5; i++) {
            System.out.println((demoService.sayHello1("admin")));
            try {
                Thread.sleep(1 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}